package com.code4lazy.shiro.Realm;

/**
 * @author geekforfree
 * DATE   2018/4/1
 */

import com.code4lazy.pojo.Admins;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import com.code4lazy.service.AdminService;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;



public class myRealm extends AuthorizingRealm{
    @Autowired
    private AdminService adminService;

    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
        //
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authentoken) throws AuthenticationException{

        String userName = (String) authentoken.getPrincipal(); // 用户名

        Admins admin = adminService.selectByName(userName);

        if(null == admin){
            throw new UnknownAccountException(); // 账号不存在
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(

                admin.getUserName(),
                // 用户名
                admin.getpWd(),
                // 密码
                getName()
                // realm name
        );
        return authenticationInfo;

    }


}
